package com.datatypes;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.widget.Toast;

import com.appengine.utils.SavingAndLoadingCenter;
import com.appengine.utils.UIUtils;
import com.appengine.utils.UserUtils;
import com.facebook.android.Facebook;
import com.facebook.android.FacebookUtils;
import com.sporteamws.SharedPref;

public class OfflineLogs {

	private List<Log> logs = null;

	public OfflineLogs() {
		this.logs = new ArrayList<Log>();
	}

	public void addLog(Log l) {
		logs.add(l);
	}

	public List<Long> getDeleteTracks() {

		List<Long> deleteTracksId = new ArrayList<Long>();

		for (Log l : logs) {
			if (!l.isPost())
				if (!l.isSync_activity()) {
					if (!l.isTrack_op())
						deleteTracksId.add(l.getTrackID());
				}
		}
		return deleteTracksId;
	}

	public List<Long> getSyncedTracks() {

		List<Long> syncTracksId = new ArrayList<Long>();

		for (Log l : logs) {
			if (!l.isPost())
				if (!l.isSync_activity()) {
					if (l.isTrack_op())
						syncTracksId.add(l.getTrackID());
				}
		}
		return syncTracksId;
	}

	public List<Long> getPosts() {

		List<Long> postTracksId = new ArrayList<Long>();

		for (Log l : logs) {
			if (l.isPost()) {
				postTracksId.add(l.getTrackID());
			}
		}
		return postTracksId;
	}

	public List<String> getSyncedActivity() {

		List<String> syncActivityId = new ArrayList<String>();

		for (Log l : logs) {
			if (!l.isPost())
				if (l.isSync_activity())
					syncActivityId.add(l.getActivityID());
		}
		return syncActivityId;
	}

	public boolean isEmpety() {
		return logs.isEmpty();
	}

	public void logsHandle_deleteTracks() {
		List<Long> del_tracks = this.getDeleteTracks();
		if (!del_tracks.isEmpty()) {
			for (long trackID : del_tracks) {
				UserUtils.DeleteTrack(trackID, SharedPref.facebookID);
			}
		}
	}

	public void logsHandle_syncTracks(Context c) {
		List<Long> sync_tracks = getSyncedTracks();
		if (!sync_tracks.isEmpty()) {
			UserTracks ut = SavingAndLoadingCenter.loadUserTracks(
					SharedPref.facebookID, c);
			for (long trackID : sync_tracks) {
				Track t = ut.getTrack(trackID);
				boolean sentSuccessfully = UserUtils.SendTrack(t,
						SharedPref.facebookID);
				if (!sentSuccessfully) {
					OfflineLogs oll = SavingAndLoadingCenter.loadOfflineLogs(
							SharedPref.facebookID, c);
					oll.addLog(new com.datatypes.Log(t.getDate(), true));
					SavingAndLoadingCenter.saveOfflineLogs(
							SharedPref.facebookID, oll, c);
				}
			}
		}
	}

	public void logsHandle_activity(Context c) {
		List<String> sync_acts = getSyncedActivity();
		if (!sync_acts.isEmpty()) {
			ActivityTracks at = SavingAndLoadingCenter.loadActivityTracks(
					SharedPref.facebookID, c);
			for (String actID : sync_acts) {
				Activity act = at.getActivity(actID);
				boolean sentSuccessfully = UserUtils.SendActivityToUser(act);
				if (!sentSuccessfully) {
					OfflineLogs oll = SavingAndLoadingCenter.loadOfflineLogs(
							SharedPref.facebookID, c);
					oll.addLog(new com.datatypes.Log(act.getActivity_id(), true));
					SavingAndLoadingCenter.saveOfflineLogs(
							SharedPref.facebookID, oll, c);
				}
			}
		}
	}

	public void logsHandle_post(Context c) {
		List<Long> sync_posts = getPosts();
		Facebook fb = new Facebook(SharedPref.facebook_appID);
		if (!sync_posts.isEmpty()) {
			UserTracks ut = SavingAndLoadingCenter.loadUserTracks(
					SharedPref.facebookID, c);
			for (long trackID : sync_posts) {
				Track track = ut.getTrack(trackID);
				FacebookUtils.PostTrackToFacebook(fb, track, c);
			}
		}
	}

}
